package cn.jy.nvr.service.impl;

import java.io.File;
import java.io.Serializable;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cn.jy.nvr.domain.ShareVideo;
import cn.jy.nvr.mapper.ShareVideoMapper;
import cn.jy.nvr.service.IShareVideoService;
@Transactional(readOnly=true,propagation=Propagation.SUPPORTS)
@Service
public class ShareVideoServiceImpl  implements IShareVideoService{
	
	@Autowired 
	ShareVideoMapper shareVideoMapper;
	
	@Transactional(readOnly=false,propagation=Propagation.REQUIRED)
	@Override
	public void add(ShareVideo shareVideo) {
		shareVideoMapper.save(shareVideo);
	}
	@Transactional	
	@Override
	public void del(Serializable ID) {
		ShareVideo shareVideo = getById(ID);
		System.out.println(shareVideo);
		String[] imagepath = shareVideo.getImagePath().split("/");
		 String  imageName=imagepath[imagepath.length-1];
		 String[] filmpath = shareVideo.getPATH().split("/");
		 String  filmName=filmpath[filmpath.length-1];
		 shareVideo.getPATH();
		System.out.println(filmName+"----"+imageName);
		 File film=new File("D:/apache-tomcat-7.0.79/webapps/Nvr/ShareVideo",filmName);
		 
		 File image=new File("D:/apache-tomcat-7.0.79/webapps/Nvr/ShareVideoImage",imageName);
		 if(film.isFile()&&film.exists()){
			 film.delete();
			 System.out.println("删除成功");
		 }
		 if (image.isFile()&&image.exists()) {
			image.delete();
			System.out.println("删除成功");
		}
		shareVideoMapper.remove(ID);
	}
	@Transactional
	@Override
	public void update(ShareVideo shareVideo) {
		shareVideoMapper.update(shareVideo);
	}

	@Override
	public ShareVideo getById(Serializable ID) {
		ShareVideo loadById = shareVideoMapper.loadById(ID);
		return loadById;
	}

	@Override
	public List<ShareVideo> getAll() {
		List<ShareVideo> loadAll = shareVideoMapper.loadAll();
		return loadAll;
	}

}
